package com.zerosoft.boilerpanel;

import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;

import java.net.URL;
import java.util.ResourceBundle;

public class ImageLabelController extends VBox implements Initializable {
    @FXML
    public VBox alarm;
    @FXML
    private Label label;
    @FXML
    private ImageView imageView;

    private String imageUrl;

    private String labelText;

    private double opacity;

    public ImageView getImageView() {
        return imageView;
    }

    public Label getLabel() {
        return label;
    }


//    public ImageLabelView(String imageUrl, String label) {
//        this.imageUrl = imageUrl;
//        this.label = label;
//        labelText.setText(label);
//        imageView.setImage(new Image(imageUrl));
//    }


    public String getImageUrl() {
        return imageView.getImage().getUrl();
    }

    public void setImageUrl(String imageUrl) {
        this.imageUrl = imageUrl;
        imageView.setImage(new Image(imageUrl));
    }

    public String getLabelText() {
        return label.getText();
    }

    public void setLabelText(String text) {
        this.labelText =text;
        label.setText(text);
    }

    @FXML
    public void blindImage(Integer time){
//        imageView.setStyle("-fx-animated: var()");
        Timeline timeline = new Timeline();
        timeline.setCycleCount(Timeline.INDEFINITE);
        timeline.setAutoReverse(true);
        opacity = imageView.getOpacity();
        KeyFrame keyFrame = new KeyFrame(javafx.util.Duration.seconds((double) time /1000),
                event -> {
                    imageView.setOpacity(opacity - imageView.getOpacity());
                }
        );

        timeline.getKeyFrames().add(keyFrame);
        timeline.play();
    }

    public void unblindImage(){
        imageView.setOpacity(opacity);
    }

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        System.out.println("ImageLabelView.initialize");
//        FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("image-label.fxml"));
//        imageView.setImage(new Image(imageUrl));
//        label.setText(labelText);
        imageView.setImage(new Image("https://t7.baidu.com/it/u=429284177,2490231864&fm=193"));
        label.setText("Helloo");
    }
}
